package com.kcl.dfss.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kcl.dfss.R;

public class CarDistanceView extends LinearLayout {
	private ImageView first_distance, sec_distance, mi;
	private int[] mNormal_Digits = { R.drawable.blue_0, R.drawable.blue_1,
			R.drawable.blue_2, R.drawable.blue_3, R.drawable.blue_4,
			R.drawable.blue_5, R.drawable.blue_6, R.drawable.blue_7,
			R.drawable.blue_8, R.drawable.blue_9 };
	private int[] mYellow_digits = { R.drawable.yellow_0, R.drawable.yellow_1,
			R.drawable.yellow_2, R.drawable.yellow_3, R.drawable.yellow_4,
			R.drawable.yellow_5, R.drawable.yellow_6, R.drawable.yellow_7,
			R.drawable.yellow_8, R.drawable.yellow_9 };
	private int[] mWarning_digits = { R.drawable.red_0, R.drawable.red_1,
			R.drawable.red_2, R.drawable.red_3, R.drawable.red_4,
			R.drawable.red_5, R.drawable.red_6, R.drawable.red_7,
			R.drawable.red_8, R.drawable.red_9 };

	public CarDistanceView(Context context) {
		super(context);
	}

	public CarDistanceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.view_distance, this);
		first_distance = (ImageView) findViewById(R.id.image_car_distance1);
		sec_distance = (ImageView) findViewById(R.id.image_car_distance2);
		mi = (ImageView) findViewById(R.id.image_mi);
	}

	/*
	 * 
	 */
	private void setFirstResource(int resId) {
		first_distance.setImageResource(resId);
	}

	private void setSecResource(int resId) {
		sec_distance.setImageResource(resId);
	}

	public void setWarningInfo(float disOrTime, int warninglevel) {
		if (disOrTime > 100.0f) {
			disOrTime = 99.0f;
		}
		int tens = (int) (disOrTime / 10);
		int digits = (int) (disOrTime % 10);
		switch (warninglevel) {
		case 0:
			setFirstResource(mNormal_Digits[tens]);
			setSecResource(mNormal_Digits[digits]);
			mi.setImageResource(R.drawable.blue_m);
			break;
		case 1:
			setFirstResource(mYellow_digits[tens]);
			setSecResource(mYellow_digits[digits]);
			mi.setImageResource(R.drawable.yellow_m);
			break;
		case 2:
			setFirstResource(mWarning_digits[tens]);
			setSecResource(mWarning_digits[digits]);
			mi.setImageResource(R.drawable.red_m);
			break;
		}

	}
}
